﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
using Telerik.Web.UI;
using System.Globalization;
using System.Web.UI.HtmlControls;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class RC_Command : System.Web.UI.Page
    {
        private SsMng ssMng = new SsMng();
        private ICommandBLL BLL = new CommandBLL();
        private IEmployeeTypeBLL etBLL = new EmployeeTypeBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            IncludeJava();

            if (!IsPostBack)
            {               
                Session["CMD_GROUP"] = Request.QueryString["grp"].ToString();

                bindCommandType();
                if (Session["CMD_GROUP"].ToString() == "QUIT")
                {
                    //คำสั่งลาออก ***********************
                    cmbCommandType.SelectedValue = "1701";
                    cmbCommandType.Enabled = false;
                }
                else if (Session["CMD_GROUP"].ToString() == "TRANSFER")
                {
                    //คำสั่งให้โอน ***********************
                    cmbCommandType.SelectedValue = "305";
                    cmbCommandType.Enabled = false;
                }
                bindEmpType();
            }            
        }

        public void IncludeJava()
        {
            HtmlGenericControl script = new HtmlGenericControl();
            script.TagName = "script";
            script.Attributes.Add("type", "text/javascript");
            script.Attributes.Add("src", "../../Scripts/jsfunction.js");
            Page.Header.Controls.Add(script);
        }
        private void bindEmpType()
        {
            IList<EmployeeTypeInfo> infoList = etBLL.ListEmployeeTypes();
            cmbEmployeeType.DataSource = infoList;
            cmbEmployeeType.DataBind();
        }
        private void bindCommandType()
        {
            string cmdGroup = "";

            switch (Request.QueryString["grp"].ToString())
            {
                case "ADD":
                    cmdGroup = "ADD";
                    break;
                case "MOV":
                    cmdGroup = "GROWMOVE";
                    break;
                case "QUIT":
                    cmdGroup = "QUIT";
                    break;
            }

            DataSet ds = null;
            DataTable dt = null;
            ds = BLL.LoadCommandType(cmdGroup);

            if (ds.Tables.Count > 0)
                dt = ds.Tables[0];

            cmbCommandType.DataSource = dt;
            cmbCommandType.DataBind();
        }
        private void LoadDataGrid()
        {
            string cmdGroup = "";

            switch (Request.QueryString["grp"].ToString())
            {
                case "ADD" :
                    cmdGroup = "ADD";
                    break;
                case "MOV" :
                    cmdGroup = "GROWMOVE";
                    break;
                case "QUIT":
                    cmdGroup = "QUIT";
                    break;
                case "TRANSFER":
                    cmdGroup = "TRANSFER";
                    break;
            }
            try
            {
                DataTable dt = null;
                dt = BLL.Select(cmdGroup, cmbCommandType.SelectedValue);

                RadGrid2.DataSource = dt;
                RadGrid2.DataBind();
            }
            catch (Exception ex)
            {

            }
        }

        protected void RadGrid2_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            
            if (e.CommandName == "Delete")
            {
                int seq = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["cmd_seq"]);
                BLL.Delete(SystemInfo._CmdEmployee, seq, ssMng.EmpSeqI);
            }
            else if (e.CommandName == "PrintPreview")
            {
                DataTable dt = new DataTable();
                int seq = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["cmd_seq"]);

                DataSet ds = BLL.LoadCommands(SystemInfo._CmdEmployee, seq, (int?)null, (int?)null);
                if (ds.Tables.Count > 0)
                    dt = ds.Tables[0];

                Session["dtReport"] = dt;
                ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "onclick", "openReport()", true);
                //return;
            }
            if (!RadGrid2.MasterTableView.IsItemInserted)
            { 
                if (string.IsNullOrEmpty(cmbCommandType.SelectedValue))
                {
                    ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "onclick", "MessageAlert('กรุณาเลือกข้อมูล [ประเภทคำสั่ง]')", true);
                    return;
                }
            }
            Session["CMDTYPE"] = cmbCommandType.SelectedValue;
            LoadDataGrid();
            
        }

        protected void RadGrid2_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            {
                if (e.Item.Cells[8].Text == "W")
                {
                    LinkButton button = e.Item.FindControl("targetControl") as LinkButton;
                    button.ForeColor = System.Drawing.Color.Red;
                }
                    
                    //e.Item.Cells[9].ForeColor = System.Drawing.Color.Red;// Drawing.Color.Red;
                
            }

            //if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            //{
            //    string strTmp;
            //    string cmd_seq,cmd_name,cmd_ref_no,approved_date;
            //    cmd_seq = e.Item.Cells[8].Text;
            //    cmd_name = e.Item.Cells[9].Text;
            //    cmd_ref_no = e.Item.Cells[10].Text;
            //    approved_date  = e.Item.Cells[11].Text;
            //    if (approved_date != "" && approved_date != "&nbsp;") 
            //    {
            //        DateTime dt;
            //        dt = Convert.ToDateTime(approved_date);
            //        approved_date = dt.ToString("dd/MM/yyyy", new CultureInfo("th-TH"));   
            //    }

            //    if (cmd_seq == "101")
            //    {
            //        strTmp = "<a href='RC_PlacementByExam.aspx?cmd_seq=" + cmd_seq + "&cmd_name=" + cmd_name + "&cmd_ref_no=" + cmd_ref_no + "&approved_date=" + approved_date + "' target='_blank'>" + e.Item.Cells[7].Text + "</a>";
            //        e.Item.Cells[7].Text = strTmp;
            //    }
                
            //    if (cmd_seq == "105")  
            //   {
            //        strTmp= "<a href='RC_TransferExam.aspx?cmd_seq=" + cmd_seq + "&cmd_name=" + cmd_name + "&cmd_ref_no=" + cmd_ref_no + "&approved_date=" + approved_date + "' target='_blank'>" + e.Item.Cells[7].Text + "</a>";
            //        e.Item.Cells[7].Text = strTmp; 
            //    }
        
            //    if (cmd_seq == "106")
            //    {
            //        strTmp = "<a href='RC_BackOfficialSoldier.aspx?cmd_seq=" + cmd_seq + "&cmd_name=" + cmd_name + "&cmd_ref_no=" + cmd_ref_no + "&approved_date=" + approved_date + "' target='_blank'>" + e.Item.Cells[7].Text + "</a>";
            //        e.Item.Cells[7].Text = strTmp;
            //    }

            //    if (cmd_seq == "107")
            //    {
            //        strTmp = "<a href='RC_BackOfficial.aspx?cmd_seq=" + cmd_seq + "&cmd_name=" + cmd_name + "&cmd_ref_no=" + cmd_ref_no + "&approved_date=" + approved_date + "' target='_blank'>" + e.Item.Cells[7].Text + "</a>";
            //        e.Item.Cells[7].Text = strTmp;
            //    }



            //    /// ยัง
            //    if (cmd_seq == "108")
            //    {
            //        strTmp = "<a href='RC_BackOfficialOld.aspx?cmd_seq=" + cmd_seq + "&cmd_name=" + cmd_name + "&cmd_ref_no=" + cmd_ref_no + "&approved_date=" + approved_date + "' target='_blank'>" + e.Item.Cells[7].Text + "</a>";
            //        e.Item.Cells[7].Text = strTmp;
            //    }

            //    // ยัง
            //    if (cmd_seq == "503")
            //    {
            //        strTmp = "<a href='RC_BackManageOld.aspx?cmd_seq=" + cmd_seq + "&cmd_name=" + cmd_name + "&cmd_ref_no=" + cmd_ref_no + "&approved_date=" + approved_date + "' target='_blank'>" + e.Item.Cells[7].Text + "</a>";
            //        e.Item.Cells[7].Text = strTmp;
            //    }



            //    if (cmd_seq == "201")
            //    {
            //        strTmp = "<a href='RC_MoveOfficialHS.aspx?cmd_seq=" + cmd_seq + "&cmd_name=" + cmd_name + "&cmd_ref_no=" + cmd_ref_no + "&approved_date=" + approved_date + "' target='_blank'>" + e.Item.Cells[7].Text + "</a>";
            //        e.Item.Cells[7].Text = strTmp;
            //    }

            //    // ยัง
            //    if (cmd_seq == "202")
            //    {
            //        strTmp = "<a href='RC_MoveOfficialPlus.aspx?cmd_seq=" + cmd_seq + "&cmd_name=" + cmd_name + "&cmd_ref_no=" + cmd_ref_no + "&approved_date=" + approved_date + "' target='_blank'>" + e.Item.Cells[7].Text + "</a>";
            //        e.Item.Cells[7].Text = strTmp;
            //    }

            //    // ยัง
            //    if (cmd_seq == "203")
            //    {
            //        strTmp = "<a href='RC_BackSamePosition.aspx?cmd_seq=" + cmd_seq + "&cmd_name=" + cmd_name + "&cmd_ref_no=" + cmd_ref_no + "&approved_date=" + approved_date + "' target='_blank'>" + e.Item.Cells[7].Text + "</a>";
            //        e.Item.Cells[7].Text = strTmp;
            //    }



            //    if (cmd_seq == "301")
            //    {
            //        strTmp = "<a href='RC_TransferOfficial.aspx?cmd_seq=" + cmd_seq + "&cmd_name=" + cmd_name + "&cmd_ref_no=" + cmd_ref_no + "&approved_date=" + approved_date + "' target='_blank'>" + e.Item.Cells[7].Text + "</a>";
            //        e.Item.Cells[7].Text = strTmp;
            //    }



            //    if (cmd_seq == "302")
            //    {
            //        strTmp = "<a href='RC_TransferQualOfficialPlus.aspx?cmd_seq=" + cmd_seq + "&cmd_name=" + cmd_name + "&cmd_ref_no=" + cmd_ref_no + "&approved_date=" + approved_date + "' target='_blank'>" + e.Item.Cells[7].Text + "</a>";
            //        e.Item.Cells[7].Text = strTmp;
            //    }



            //    if (cmd_seq == "303")
            //    {
            //        strTmp = "<a href='RC_TransferOfficialPlus.aspx?cmd_seq=" + cmd_seq + "&cmd_name=" + cmd_name + "&cmd_ref_no=" + cmd_ref_no + "&approved_date=" + approved_date + "' target='_blank'>" + e.Item.Cells[7].Text + "</a>";
            //        e.Item.Cells[7].Text = strTmp;
            //    }


            //    if (cmd_seq == "304")
            //    {
            //        strTmp = "<a href='RC_TransferExamDetail.aspx.aspx?cmd_seq=" + cmd_seq + "&cmd_name=" + cmd_name + "&cmd_ref_no=" + cmd_ref_no + "&approved_date=" + approved_date + "' target='_blank'>" + e.Item.Cells[7].Text + "</a>";
            //        e.Item.Cells[7].Text = strTmp;
            //    }


            //    if (cmd_seq == "502")
            //    {
            //        strTmp = "<a href='RC_TransferOfficialManage.aspx?cmd_seq=" + cmd_seq + "&cmd_name=" + cmd_name + "&cmd_ref_no=" + cmd_ref_no + "&approved_date=" + approved_date + "' target='_blank'>" + e.Item.Cells[7].Text + "</a>";
            //        e.Item.Cells[7].Text = strTmp;
            //    }


            //    if (cmd_seq == "401")
            //    {
            //        strTmp = "<a href='RC_PromoteOfficial.aspx?cmd_seq=" + cmd_seq + "&cmd_name=" + cmd_name + "&cmd_ref_no=" + cmd_ref_no + "&approved_date=" + approved_date + "' target='_blank'>" + e.Item.Cells[7].Text + "</a>";
            //        e.Item.Cells[7].Text = strTmp;
            //    }


            //    if (cmd_seq == "501")
            //    {
            //        strTmp = "<a href='RC_MoveOfficial.aspx?cmd_seq=" + cmd_seq + "&cmd_name=" + cmd_name + "&cmd_ref_no=" + cmd_ref_no + "&approved_date=" + approved_date + "' target='_blank'>" + e.Item.Cells[7].Text + "</a>";
            //        e.Item.Cells[7].Text = strTmp;
            //    }


            //    // ยัง
            //    if (cmd_seq == "601")
            //    {
            //        strTmp = "<a href='RC_OifficialSalaryQual.aspx?cmd_seq=" + cmd_seq + "&cmd_name=" + cmd_name + "&cmd_ref_no=" + cmd_ref_no + "&approved_date=" + approved_date + "' target='_blank'>" + e.Item.Cells[7].Text + "</a>";
            //        e.Item.Cells[7].Text = strTmp;
            //    }
      
            //}

        }

        protected void cmbEmployeeType_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            Session["EMPTYPE"] = cmbEmployeeType.SelectedValue;
            LoadDataGrid();
        }
        protected void cmbCommandType_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            Session["CMDTYPE"] = cmbCommandType.SelectedValue;
            LoadDataGrid();
        }
        protected void RadManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {

        }
    }
}
